/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.mto.module.karaoke.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.mto.module.karaoke.model.Song;
import com.mto.module.karaoke.portlet.util.comparator.SongComparatorFactoryUtil;
import com.mto.module.karaoke.service.base.SongLocalServiceBaseImpl;

/**
 * The implementation of the song local service.
 * 
 * <p>
 * All custom service methods should be put in this class. Whenever methods are
 * added, rerun ServiceBuilder to copy their definitions into the
 * {@link com.mto.module.karaoke.service.SongLocalService} interface.
 * 
 * <p>
 * This is a local service. Methods of this service will not have security
 * checks based on the propagated JAAS credentials because this service can only
 * be accessed from within the same VM.
 * </p>
 * 
 * @author vuonglm
 * @see com.mto.module.karaoke.service.base.SongLocalServiceBaseImpl
 * @see com.mto.module.karaoke.service.SongLocalServiceUtil
 */
public class SongLocalServiceImpl extends SongLocalServiceBaseImpl {
	
	private static final Log LOG = LogFactoryUtil.getLog(SongLocalServiceImpl.class);
	
	/*
	 * NOTE FOR DEVELOPERS:
	 * 
	 * Never reference this interface directly. Always use {@link
	 * com.mto.module.karaoke.service.SongLocalServiceUtil} to access the song
	 * local service.
	 */

	public List<Song> search(Song song, int start, int end, String orderByCol, String orderByType) {
		OrderByComparator orderByComparator = SongComparatorFactoryUtil.createOrderByComparator(orderByCol, orderByType);
		try {
			if(song == null) {
				return songPersistence.findAll(start, end, orderByComparator);
			}
			long recordNumber = song.getRecordNumber();
			String songName = song.getSongName();
			if(StringUtils.isBlank(songName)) songName = null;
			String songAuthor = song.getSongAuthor();
			if(StringUtils.isBlank(songAuthor)) songAuthor = null;
			Integer songCategory = song.getSongCategory();
			if(songCategory == 0) songCategory = null;
			String songSinger = song.getSongSinger();
			if(StringUtils.isBlank(songSinger)) songSinger = null;
			return songFinder.search(songName, songAuthor, songCategory, songSinger, recordNumber, start, end, orderByComparator);
		} catch (SystemException e) {
			LOG.error(e, e);
		}
		return null;
	}
	
	public Integer searchCount(Song song) {
		try {
			if(song == null) {
				return songPersistence.countAll();
			}
			long recordNumber = song.getRecordNumber();
			String songName = song.getSongName();
			if(StringUtils.isBlank(songName)) songName = null;
			String songAuthor = song.getSongAuthor();
			if(StringUtils.isBlank(songAuthor)) songAuthor = null;
			Integer songCategory = song.getSongCategory();
			if(songCategory == 0) songCategory = null;
			String songSinger = song.getSongSinger();
			if(StringUtils.isBlank(songSinger)) songSinger = null;
			
			return songFinder.searchCount(songName, songAuthor, songCategory, songSinger, recordNumber);
		} catch (SystemException e) {
			LOG.error(e, e);
		}
		return -1;
	}
}